﻿using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Read.API.Application.Command.RegistrationCommand;
using D.Medicalcare.Read.API.Dtos;
using MediatR;

namespace D.Medicalcare.Read.API.Application.Handler.RegistrationHandler
{
    /// <summary>
    /// 获取挂号列表
    /// </summary>
    public class QueryRegistrationHandler : IRequestHandler<QueryRegistrationCommand, APIResult<RegistrationDTO>>
    {
        private readonly IBaseRepository<Registration> rrep;
        private readonly IBaseRepository<DoctorScheduling> srep;
        private readonly IBaseRepository<Doctor> drep;
        private readonly IBaseRepository<Department> prep;
        private readonly IBaseRepository<TitleManagement> trep;
        private readonly IBaseRepository<HospitalNavigation> hrep;
        private readonly IBaseRepository<Patient> patientrep;
        private readonly ILogger<QueryRegistrationHandler> logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="rrep"></param>
        /// <param name="srep"></param>
        /// <param name="drep"></param>
        /// <param name="prep"></param>
        /// <param name="trep"></param>
        /// <param name="hrep"></param>
        /// <param name="patientrep"></param>
        public QueryRegistrationHandler(IBaseRepository<Registration> rrep,IBaseRepository<DoctorScheduling> srep,
            IBaseRepository<Doctor> drep,IBaseRepository<Department> prep,IBaseRepository<TitleManagement> trep,
            IBaseRepository<HospitalNavigation> hrep,IBaseRepository<Patient> patientrep,ILogger<QueryRegistrationHandler> logger)
        {
            this.rrep = rrep;
            this.srep = srep;
            this.drep = drep;
            this.prep = prep;
            this.trep = trep;
            this.hrep = hrep;
            this.patientrep = patientrep;
            this.logger = logger;
        }
        /// <summary>
        /// 处理
        /// </summary>
        public Task<APIResult<RegistrationDTO>> Handle(QueryRegistrationCommand request, CancellationToken cancellationToken)
        {
            APIResult<RegistrationDTO> result = new APIResult<RegistrationDTO>();
            try
            {
                var m = from a in rrep.GetAll()
                        join b in drep.GetAll() on a.DoctorId equals b.DoctorId into doctorJoin
                        from b in doctorJoin.DefaultIfEmpty()
                        join c in srep.GetAll() on b.DoctorId equals c.DoctorId into schedulingJoin
                        from c in schedulingJoin.DefaultIfEmpty()
                        join d in prep.GetAll() on b.DeparId equals d.DeparId into departmentJoin
                        from d in departmentJoin.DefaultIfEmpty()
                        join e in trep.GetAll() on b.TitlemanagementId equals e.TitleManagementId into titleJoin
                        from e in titleJoin.DefaultIfEmpty()
                        join f in hrep.GetAll() on d.CampusId equals f.HospitalNavigationId into hospitalJoin
                        from f in hospitalJoin.DefaultIfEmpty()
                        join g in patientrep.GetAll() on a.PatientId equals g.PatientId into patientJoin
                        from g in patientJoin.DefaultIfEmpty()
                        select new RegistrationDTO
                        {
                            RegistrationId = a.RegistrationId,
                            CampusName = f.CampusName,
                            AddressCampus = f.AddressCampus,
                            PatientName = g.PatientName,
                            PatientNo = g.PatientNo,
                            DocumentNo = g.DocumentNo,
                            Phone = g.Phone,
                            DeparName = d.DeparName,
                            DoctorName = b.DoctorName,
                            JobName = e.JobName,
                            Registrationfee = b.Registrationfee,
                            RegistrationType = a.RegistrationType,
                            Statue = a.Statue,
                            RegistrationTime = a.RegistrationTime,
                            Mtime = a.Mtime,
                            WaitTime = a.WaitTime,
                            UpdateTime = a.UpdateTime,
                            UpdateReson = a.UpdateReson,
                            Doctorlocation = d.Doctorlocation
                        };
                result.Code = APIEnum.Success;
                result.Msg = "查询成功";
                logger.LogInformation("查询成功");
                result.Data = m.FirstOrDefault(x => x.RegistrationId == request.RegistrationId);
                return Task.FromResult(result);

            }
            catch (Exception ex)
            {
                logger.LogError("查询异常" + ex.Message);
                throw;
            }
           
            
        }
    }
}
